﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace WMS.Seven.BaseService
{
    public class BaseAppService<T, K> : ApplicationService where T : class, IEntity<int>
    {
        private readonly IRepository<T, int> _repository;

        public BaseAppService(IRepository<T, int> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<T> AddAsync(T entity)
        {
            return await _repository.InsertAsync(entity);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="orderby">排序字段</param>
        /// <param name="detailEntity">返回的实体是否包括其子实体或相关的详细信息</param>
        /// <returns></returns>
        public async Task<PagedResult<T>> GetListAsync<M>(int pageIndex, int pageSize, string orderby, bool detailEntity, Expression<Func<M, bool>> filter)
        {
            IQueryable<T>? resultQueryable = await _repository.GetQueryableAsync();
            var result = resultQueryable.Where(filter);
            return result.PageResult(pageIndex, pageSize);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(K entity)
        {
            try
            {
                var needEntity = ObjectMapper.Map<K, T>(entity);
                await _repository.DeleteAsync(needEntity);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(T model)
        {
            try
            {
                await _repository.DeleteAsync(model);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<T> UpdateAsync(K entity)
        {
            var needEntity = ObjectMapper.Map<K, T>(entity);
            return await _repository.UpdateAsync(needEntity);
        }
        /// <summary>
        /// 唯一性验证
        /// </summary>
        /// <typeparam name="M"></typeparam>
        /// <param name="filter"></param>
        /// <returns></returns>
        public async Task<bool> HasOnly(string filter, string param)
        {
            IQueryable<T>? resultQueryable = await _repository.GetQueryableAsync();
            var result = resultQueryable.Any(filter, param);
            return result;
        }
    }
}
